package com.jf3q.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.common.domain.EduMember;
import com.jf3q.common.exception.JfException;
import com.jf3q.common.mapper.EduMemberMapper;
import com.jf3q.common.utils.SaltPassUtils;
import com.jf3q.front.dto.MemberRegDto;
import com.jf3q.front.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.Base64;
import java.util.Date;

@Service
public class MemberServiceImpl extends ServiceImpl<EduMemberMapper, EduMember> implements MemberService {
    @Value("${member.avatar}")
    private String defaultAvatar;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public void reg(MemberRegDto registerVo) {
        //获取注册的数据
        String imgCode = registerVo.getImgCode(); //图片验证码
        String imgKey = registerVo.getImgKey(); //图片验证码
        String smsCode = registerVo.getSmsCode(); //短信验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if(StringUtils.isEmpty(imgKey) ||StringUtils.isEmpty(imgCode) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(smsCode) || StringUtils.isEmpty(nickname)) {
            throw new JfException(20001,"注册失败-必填项没填写完整");
        }
        //判断手机号是否已经被注册了
        long count = this.count(new LambdaQueryWrapper<EduMember>().eq(EduMember::getMobile, mobile));
        if(count>0){
            throw new JfException(20002,"注册失败-该手机号已注册请直接登录");
        }


        //判断图片验证码
        String imgStr = stringRedisTemplate.opsForValue().get(imgKey);

        if(StringUtils.isEmpty(imgStr) || !imgStr.equals(imgCode)){
            throw new JfException(20003,"注册失败-图形验证码输入错误或已超2分钟");

        }

        //判断短信验证码
        String redisSmsCode = stringRedisTemplate.opsForValue().get(mobile);
        if(StringUtils.isEmpty(redisSmsCode) || !redisSmsCode.equals(smsCode)){
            throw new JfException(20004,"注册失败-短信验证码输入错误或已超2分钟");

        }

        //数据添加数据库中
        EduMember member = new EduMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        byte[] salt = SaltPassUtils.generateSalt();
        String saltStr = Base64.getEncoder().encodeToString(salt);
        member.setSalt(saltStr);
        member.setPassword(SaltPassUtils.hashPassword(password,salt));//密码需要加盐加密的
        member.setIsDisabled(0);//用户不禁用
        member.setAvatar(defaultAvatar);//设置一个默认头像
        member.setEduCreate(new Date());
        member.setEduModified(new Date());
        baseMapper.insert(member);

    }
}
